package hw_7_31;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class serializable_Demo1 {
    private static final String FILE_PATH = "data/a.dat";
    public static void main(String[] args) {
        List<Object> objectList = Arrays.asList(
                new Student("小王", 18),
                new Student("小张", 19),
                new Student("小李", 20),
                new Teacher("王老师", "数学"),
                new Teacher("张老师", "物理"),
                new Teacher("李老师", "英语")
        );
        //对象序列化
        serializeObjects(objectList);
        System.out.println("对象序列化完成！");
        //对象反序列化
        List<Object> newObjList = deserializeStudents();
        System.out.println("学生信息反序列化完成！");
        System.out.println(newObjList.size());

        for (Object obj : newObjList) {
            if(obj instanceof Student) {
                Student student = (Student) obj;
                System.out.println("学生姓名：" + student.getName() + ",学生年龄：" + student.getAge());
            }
            else if(obj instanceof Teacher) {
                Teacher teacher = (Teacher)obj;
                System.out.println("教师姓名：" + teacher.getName() + ",教授科目：" + teacher.getSubject());
            }
        }
    }

    public static void serializeObjects(List<Object> objectList){
        File file = new File(FILE_PATH);
        if (! file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(objectList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<Object> deserializeStudents(){
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(FILE_PATH)))) {
            return (List<Object>)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

class Student implements Serializable {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Teacher implements Serializable{
    private String name;
    private String subject;

    public Teacher() {
    }

    public Teacher(String name, String subject) {
        this.name = name;
        this.subject = subject;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}


